package com.indusfo.spc.client.service.impl;

import java.util.List;

import org.hibernate.QueryException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.indusfo.spc.client.mapper.ClientAnoDetailMapper;
import com.indusfo.spc.client.pojo.ClientAnoDetail;
import com.indusfo.spc.client.service.ClientAnoDetailService;
import com.indusfo.spc.exception.ParamsErrorException;

/**
 * 客户端不良明细实现类
 * 
 * @author zhengd
 */
@Service
public class ClientAnoDetailServiceImpl implements ClientAnoDetailService {
	@Autowired
	private ClientAnoDetailMapper clientAnoDetailMapper;

	/**
	 * 根据批号查询所有不良明细
	 */
	@Override
	public List<ClientAnoDetail> queryClientAnoDetail(Integer batchId) {
		return clientAnoDetailMapper.queryClientAnoDetail(batchId);
	}

	/**
	 * 保存不良原因和对策
	 */
	@Override
	public Integer saveClientAnoDetail(ClientAnoDetail clientAnoDetail) {
		if (clientAnoDetail.getBadcauseId() == null) {
			throw new ParamsErrorException("请传入不良原因ID");
		}
		if (clientAnoDetail.getBadreplyId() == null) {
			throw new ParamsErrorException("请传入不良对策ID");
		}
		if (clientAnoDetail.getUserId() == null) {
			throw new ParamsErrorException("请传入维修人员ID");
		}
		if (clientAnoDetail.getStationId() == null) {
			throw new ParamsErrorException("请传入维修工位ID");
		}
		if (clientAnoDetail.getProId() == null) {
			throw new ParamsErrorException("请传入维修工序ID");
		}
		Integer count = clientAnoDetailMapper.saveClientAnoDetail(clientAnoDetail);
		if (count == 0) {
			throw new QueryException("保存失败！");
		}
		return count;
	}
}
